package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.service.GeneralService
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.dao.DataAccessException
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.PreparedStatementSetter
import org.springframework.jdbc.core.ResultSetExtractor

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.sql.DataSource
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException

class apiHSTStateUpdate implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiHSTStateUpdate.class);

    private static final String DATA_SOURCE_NAME = "dataSource";



    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        try {

            String getSwitchSQL = "select cr.chat_session_id,cr.id from chat_request cr \n" +
                    "left join  chat_message cm on cr.chat_session_id=cm.chat_session_id\n" +
                    "where  cm.chat_session_id is not null and (cr.request_status_id=4 or cr.request_status_id=7)\n" +
                    "GROUP BY cr.chat_session_id,cr.id\n" +
                    "ORDER BY 1 desc";
            List getSwitchSQLResult = eliteBean.chaxun_sql(getSwitchSQL, "dataSource");
            log.debug("异常数据：" + getSwitchSQLResult.toString());
            log.debug("异常条数：" + getSwitchSQLResult.size());

            int c=0;
            if (getSwitchSQLResult.size() > 0) {
                for (int i = 0; i < getSwitchSQLResult.size(); i++) {

                    Map getSwitchMap = (Map) getSwitchSQLResult.get(i);

                    String Session_Id = getSwitchMap.get("chat_session_id");
                    String Id = getSwitchMap.get("id");
                    if (Session_Id) {

                        String UpSql = "update chat_request set request_status_id=1 where id=" + Id + "  and chat_session_id=" + Session_Id;

                        c= eliteBean.update(UpSql, "dataSource")
                    }

                    log.debug("修复条数：" + c++);
                }

            }
        } catch (Exception e) {
            log.error("状态修改定时任务操作异常", e);
            return 0;
        }
        return 1
    }
}